﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.Finance.BankSys;
using TLL.UTL;
using TLL.Finance.Currencys;

namespace TasksLog
{
    public partial class RegMerchant : Form
    {
        public RegMerchant()
        {
            InitializeComponent();
            InitializeData();
        }
        public void InitializeData()
        {
            ClearData();
            int index = 0;
            using (var repository = new MerchantRepository(Parametrs.DebugURL, Parametrs.CodingKey))
                foreach (var merchant in repository.Get())
                {
                    LBName.Items.Add(merchant.Name);
                    LBCardNumber.Items.Add(merchant.Card);
                    LBPassword.Items.Add(merchant.Password);
                    LBCurrency.Items.Add(merchant.CurrencyType.ToString());

                    var bt = new Button()
                    {
                        BackColor = Color.Red,
                        FlatStyle = FlatStyle.Flat,
                        ForeColor = Color.Black,
                        Text = "DEL",
                        Height = 16,
                        Top = 16 * index + 2,
                        Font = new Font(FontFamily.GenericSansSerif, 6, FontStyle.Bold, GraphicsUnit.Pixel),
                        Width = 50,
                        Tag = merchant.ID,
                        Left = 4
                    };
                    bt.Click += this.BtDelete_Click;

                    PnControl.Controls.Add(bt);

                    TTNotiseDelete.SetToolTip(bt, "delete");

                    index++;
                }
        }

        private void BtAdd_Click(object sender, EventArgs e)
        {
            using (var repository = new MerchantRepository(Parametrs.DebugURL, Parametrs.CodingKey))
                if (repository.Access)
                    repository.Create(new BankMerchant(
                        int.Parse(TBID.Text),
                        TBName.Text,
                        TBCardNumber.Text,
                        TBPassword.Text,
                        Utilities.GetEnum<CurrencyType>(CBCurrency.SelectedItem as string)
                        ));
            InitializeData();
        }
        private void ClearData()
        {
            LBName.Items.Clear();
            LBCardNumber.Items.Clear();
            LBPassword.Items.Clear();
            LBCurrency.Items.Clear();
            PnControl.Controls.Clear();
        }
        private void BtDelete_Click(object sender, EventArgs e)
        {
            using (var repository = new MerchantRepository(Parametrs.DebugURL, Parametrs.CodingKey))
                if (repository.Access)
                    repository.Delete((int)((sender as Button).Tag));

            InitializeData();
        }
    }
}
